﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using ERP.View;
using ERP.Reports;
using ERP.Hoadon;
namespace ERP.Hoadon
{
    public partial class viewErp_Ql_Hoadon_Tructiep : DevExpress.XtraEditors.XtraForm
    {
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public int flag = 0;
        public int count = 0;
        public cs_erp_hoadonInfo hoadon = new cs_erp_hoadonInfo();
        public cs_erp_nhapkhoInfo nk = new cs_erp_nhapkhoInfo();

        public viewErp_Ql_Hoadon_Tructiep()
        {
            InitializeComponent();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            string condition = "";
            int i = 0;
            if (this.ckHoten.Checked == true)
            {
                i = 1;
                condition = " b.tenkhachhang like N'%" + this.txtHoten.Text.Trim() + "%'";

                this.Load_Data_by_Condition(i, condition);
                this.grcDanhsach.RefreshDataSource();
            }
            if (this.ckNgay.Checked == true)
            {
                DateTime tn = Convert.ToDateTime(this.txtTungay.EditValue);
                DateTime dn = Convert.ToDateTime(this.txtDenngay.EditValue);
                int tn1 = tn.Year * 12 * 30 + tn.Month * 30 + tn.Day;
                int tn2 = dn.Year * 12 * 30 + dn.Month * 30 + dn.Day;
                i = 2;
                condition = " year(a.ngayban)*12*30 + month(a.ngayban)*30 + day(a.ngayban) between " + tn1 + " and " + tn2;
                this.Load_Data_by_Condition(i, condition);
                this.grcDanhsach.RefreshDataSource();
            }
            if (this.ckDienthoai.Checked == true)
            {
                i = 3;
                condition = " b.dienthoai like N'%" + this.txtDienthoai.Text.Trim() + "%'";
                this.Load_Data_by_Condition(i, condition);
                this.grcDanhsach.RefreshDataSource();
            }
        }

        private void viewErp_Ql_Hoadon_Tructiep_Load(object sender, EventArgs e)
        {
            this.txtTungay.EditValue = DateTime.Now.AddDays(1 - DateTime.Now.Day);
            this.txtDenngay.EditValue = DateTime.Now;
            this.txtHoten.Focus();
            this.Load_Data();
            this.CheckUser();
        }

        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnMoi.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                    this.btnXoa.Enabled = false;
                }
                if (userright.updatedata == 0)
                {
                    this.btnDieuchinh.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }


        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            ds = this.hoadon.Get_List_BHTT();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }

        private void Load_Data_by_Condition(int i, string condition)
        {
            DataSet ds = new DataSet("ds");
            ds = this.hoadon.Get_List_by_Condition_BHTT(i, condition);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }

        private void btnChitietHD_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() > 0)
                {
                    viewErp_Nhaphoadon_Tructiep nhd = new viewErp_Nhaphoadon_Tructiep();
                    nhd.user = this.user;
                    nhd.flag = 3;
                    nhd.hoadon = this.hoadon;
                    nhd.MdiParent = this.MdiParent;
                    nhd.Show();
                }
            }
        }

        private void btnDieuchinh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() > 0 && this.hoadon.trangthai == 1)
                {
                    viewErp_Nhaphoadon_Tructiep nhd = new viewErp_Nhaphoadon_Tructiep();
                    nhd.user = this.user;
                    nhd.flag = 2;
                    nhd.hoadon = this.hoadon;
                    nhd.MdiParent = this.MdiParent;
                    nhd.Show();
                }
                else
                {
                    XtraMessageBox.Show("Hóa đơn đã được xác nhận, không thể điều chỉnh!", "Thông báo");
                }
            }
        }

        private void btnXoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() > 0 && (this.hoadon.trangthai == 1 || this.hoadon.trangthai == 2))
                {
                    DialogResult dg = new DialogResult();
                    dg = MessageBox.Show("Bạn chắc chắn muốn xóa hóa đơn đã chọn?", "Cảnh báo...", MessageBoxButtons.YesNo);
                    if (dg == DialogResult.Yes)
                    {
                        this.hoadon.ExcuteProcedure(3);
                        this.Load_Data();
                    }
                }
            }
        }

        private void btnThoat_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.Dispose(true);
        }

        private void btnIn_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.inHoadon();
        }

        private void btnDatcoc_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() > 0 && this.hoadon.conno > 0)
                {
                    viewERP_Hoadon_Thanhtoan gv = new viewERP_Hoadon_Thanhtoan();
                    gv.user = this.user;
                    //gv.kh = kh;
                    gv.hoadon = this.hoadon;
                    gv.ShowDialog();
                    this.Load_Data();
                }
                else
                {
                    XtraMessageBox.Show("Hóa đơn này đã thanh toán hết tiền nợ!", "Thông báo");
                }
            }
        }

        private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() > 0)
                {
                    viewERP_LichsuThanhtoan gv = new viewERP_LichsuThanhtoan();
                    gv.user = this.user;
                    //gv.kh = kh;
                    gv.hoadon = this.hoadon;
                    gv.ShowDialog();
                    this.Load_Data();
                }
                //else
                //{
                //    XtraMessageBox.Show("Hóa đơn này đã thanh toán hết tiền nợ!", "Thông báo");
                //}
            }
        }

        private void btnDoanhthu_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            viewErp_Doanhthu vdt = new viewErp_Doanhthu();
            vdt.user = this.user;
            vdt.ShowDialog();
        }

        private void inHoadon()
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() <= 0)
                {
                    return;
                }

                cs_erp_khachhangInfo kh = new cs_erp_khachhangInfo();
                kh.id_kh = this.hoadon.id_kh;
                kh.Get_Info();
                DataSet ds = new DataSet("ds");
                string ban1 = kh.tenkhachhang + ", Ngày: " + DateTime.Now.ToShortDateString();
                cs_erp_chitiet_hoadonInfo ct = new cs_erp_chitiet_hoadonInfo();
                ct.id_hoadon = this.hoadon.id_hd;
                ds = ct.Get_List();
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("Không có số liệu...", "Thông báo!");
                    return;
                }
                cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
                dv.captren = "0";
                dv.Get_Info_by_Parents();
                rptDoanhthu rpt = new rptDoanhthu();
                rpt.DataSource = ds.Tables[0];
                rpt.ds_BaoCao = ds.Tables[0];
                rpt.dsHoadon = this.hoadon.Get_HD_Tinhtien().Tables[0];
                rpt.ban = ban1;
                rpt.donvi = dv.tendonvi;
                rpt.diachi = dv.diachi + " - " + dv.sodt_fax;
                rpt.In();
                rpt.ShowPreview();
            }
        }

        private void btnGiaohang_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() <= 0)
                {
                    return;
                }
                DialogResult dg = new DialogResult();
                dg = MessageBox.Show("Bạn chắc chắn muốn Xác nhận Bán hàng cho hóa đơn đã chọn?", "Cảnh báo...", MessageBoxButtons.YesNo);
                if (dg == DialogResult.Yes)
                {
                    viewErp_Xacnhan_BHTT xn = new viewErp_Xacnhan_BHTT();
                    xn.hoadon = this.hoadon;
                    xn.user = this.user;
                    xn.ShowDialog();
                    this.Load_Data();
                }
            }
        }

        private void txtHoten_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.btnTim_Click(sender, e);
            }
        }

        private void txtTungay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.btnTim_Click(sender, e);
            }
        }

        private void txtDenngay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.btnTim_Click(sender, e);
            }
        }

        private void txtDienthoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.btnTim_Click(sender, e);
            }
        }

        
    }
}